{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import pandas as pd\n",
    "from plotly.graph_objs import *\n",
    "import os\n",
    "import datetime\n",
    "ts.set_token('ac8805887aa21d24fd5ec47a4ae9e7a09804ebaeedea93352a7b13d2e8f08e46')\n",
    "start1 = '2017-05-01'\n",
    "end1 = '2017-07-07'\n",
    "global DETA\n",
    "DETA = 0.2\n",
    "\n",
    "# 这里定义一个函数，可以应用到每一行，设置axis=1就是应用到每一行的意思；如果设置axis=0，就是应用于每一列的意思\n",
    "def openMinusLow(x):\n",
    "    return float(x['open'])-float(x['low'])\n",
    "\n",
    "# 获取股票的名称\n",
    "def get_stock_name(stock_code):\n",
    "    df = ts.get_realtime_quotes(stock_code)\n",
    "    return df['name'][0]\n",
    "\n",
    "def get_stock_operate_date(stock_code):\n",
    "    print('统计股票：'+stock_code\n",
    "          +'(' + get_stock_name(stock_code) + ')'\n",
    "          +'\\n* 当天上涨率高于2%的天数。')\n",
    "    print('* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价')\n",
    "    print('        然后在看price_change，假设高于0.3以上，400股就能赚100块以上')\n",
    "    print(         'price_change=收盘价-昨收价，p_change=price_change/昨收价*100%')\n",
    "    print('-------------------------------------------------------------------------------')\n",
    "    start0 = '2017-05-02' # 起始日期，可以修改\n",
    "    end0 = str(datetime.date.today())    # 直到今天\n",
    "    df_h_data = ts.get_hist_data(stock_code,start=start0,end=end0)\n",
    "    # print(df_h_data[['open','high','close','low','volume','price_change', 'p_change']].head(2))\n",
    "    # 获取收盘价比昨天收盘价高0.1以上，并且开盘价低于收盘价的日期；\n",
    "    global DETA\n",
    "    df_price_change_list = df_h_data[\n",
    "        (df_h_data['price_change'] > DETA)\n",
    "        &(df_h_data['open'] < df_h_data['close'])\n",
    "    ]\n",
    "    df_temp = df_price_change_list.apply(openMinusLow, axis=1) # 对每一行执行函数\n",
    "    # 用insert就没有SettingWithCopyWarning警告了\n",
    "    df_price_change_list.insert(0,'open-low',df_temp)\n",
    "    print(df_price_change_list.index)\n",
    "    return df_price_change_list\n",
    "\n",
    "# 获取可以操作的交易日里面的买入点时间\n",
    "def get_low_price_moment(stock_code, df_stock_operate):\n",
    "    for i_date in df_stock_operate.index:\n",
    "        \n",
    "        #获取某一天的历史价格变动情况，好像间隔周期是3秒\n",
    "        df_tick_data = ts.get_tick_data(stock_code,date=i_date) \n",
    "        lowPrice = float(df_stock_operate['low'][i_date])\n",
    "        highPrice = float(df_stock_operate['high'][i_date])\n",
    "        print('--------------------Data: '+ i_date+'--------------------')\n",
    "        print('--------------------LowestPrice :'+str(lowPrice)+'---------------------')\n",
    "        print('--------------------HighestPrice :'+str(highPrice)+'-------------------')\n",
    "        print(df_tick_data.head(2))\n",
    "        print('...... total: '+str(len(df_tick_data)))\n",
    "        \n",
    "        df_moment = df_tick_data[\n",
    "            (df_tick_data['price']-lowPrice < 0.05)\n",
    "        ]\n",
    "        size = len(df_moment)\n",
    "        if size != 0:\n",
    "            #print('df_moment.size = '+str(size))\n",
    "            #print(df_moment.iloc[0,0]) #iloc[column, row]，不需要知道行号和列号，直接获取单位元素\n",
    "            #print(df_moment.iloc[size-1,0])\n",
    "            datetimeEnd = df_moment.iloc[0,0]\n",
    "            datatimeStart = df_moment.iloc[size-1,0]\n",
    "            print('--->从 '+datatimeStart+ ' 到 '+ datetimeEnd + '有合适的时间点：'+str(size)+'个')\n",
    "            print(df_moment)\n",
    "        else:\n",
    "            print('df_moment is empty')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计股票：600613(神奇制药)\n",
      "* 当天上涨率高于2%的天数。\n",
      "* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价\n",
      "        然后在看price_change，假设高于0.3以上，400股就能赚100块以上\n",
      "price_change=收盘价-昨收价，p_change=price_change/昨收价*100%\n",
      "-------------------------------------------------------------------------------\n",
      "Index(['2017-06-07'], dtype='object', name='date')\n",
      "HTTP Error 456: \n",
      "HTTP Error 456: \n",
      "HTTP Error 456: \n"
     ]
    },
    {
     "ename": "OSError",
     "evalue": "获取失败，请检查网络和URL",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-43-ac9ced5cfb0d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mstock_code1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'600613'\u001b[0m \u001b[1;31m# 先测试一下\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mget_low_price_moment\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstock_code1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mget_stock_operate_date\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstock_code1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-42-75019cbfa3c4>\u001b[0m in \u001b[0;36mget_low_price_moment\u001b[1;34m(stock_code, df_stock_operate)\u001b[0m\n\u001b[0;32m     48\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     49\u001b[0m         \u001b[1;31m#获取某一天的历史价格变动情况，好像间隔周期是3秒\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m         \u001b[0mdf_tick_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mts\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_tick_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstock_code\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mi_date\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     51\u001b[0m         \u001b[0mlowPrice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_stock_operate\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'low'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi_date\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     52\u001b[0m         \u001b[0mhighPrice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_stock_operate\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'high'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi_date\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files (x86)\\python3\\lib\\site-packages\\tushare\\stock\\trading.py\u001b[0m in \u001b[0;36mget_tick_data\u001b[1;34m(code, date, retry_count, pause, src)\u001b[0m\n\u001b[0;32m    183\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    184\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 185\u001b[1;33m     \u001b[1;32mraise\u001b[0m \u001b[0mIOError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mct\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mNETWORK_URL_ERROR_MSG\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    186\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    187\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mOSError\u001b[0m: 获取失败，请检查网络和URL"
     ]
    }
   ],
   "source": [
    "stock_code1 = '600613' # 先测试一下\n",
    "get_low_price_moment(stock_code1, get_stock_operate_date(stock_code1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
